<?php
$alias = isset($_GET['alias'])?$_GET['alias']:null;
if(!$alias){
	header('location: index.php');
	exit;
}
$ajax = isset($_GET['ajax'])?$_GET['ajax']:null;
include_once 'constants.php';
$mysqli = new mysqli($hostname,$username,$password,$database);
$result = getLastLoc($mysqli,$alias);
if($result==NULL||count($result)==0){
	header('HTTP/1.0 900 general error');
	exit;
}
if($ajax){
	echo 0;
	return;
}
function getLastLoc($mysqli,$alias){
$sql = 'SELECT date_format(b.insertTs,\'%b %e, %Y %l:%i%p\') as ts, latitude, longitude, comment from loc_cust a, loc_location b '
	.'where alias = ? and a.pkey = fk_pkey_loc_cust '
	.'and b.pkey = (select max(pkey) from loc_location where '
	.'fk_pkey_loc_cust = a.pkey)';
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s',$alias);	
$stmt->execute();
$stmt->bind_result($ts,$lat,$lng,$comment);
if($stmt->fetch())return array($ts,$lat,$lng,$comment);
return null;	
}
echo $htmlHead;
?>
<script>
var gMapsLoaded = false;
window.gMapsCallback = function(){
    gMapsLoaded = true;
    $(window).trigger('gMapsLoaded');
}
window.loadGoogleMaps = function(){
    if(gMapsLoaded) return window.gMapsCallback();
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src","http://maps.google.com/maps/api/js?sensor=false&callback=gMapsCallback");
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
}
clickIt();
function clickIt(){
    function initialize(){
    	var myLatlng = new google.maps.LatLng(<?php echo $result[1].','.$result[2];?>);
        var mapOptions = {
            zoom: 14,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP};
        map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
    	var marker = new google.maps.Marker({
    		position : myLatlng,
    		map : map,
    		draggable : false
    	});
    	var contentString = '<b style="width:50%;height:50%;">'+
    	  '<?php echo $result[0]."<br/>".$result[3];?></b>';
    	    	
    	var infowindow = new google.maps.InfoWindow({
    		  content:contentString
    	});

    	infowindow.open(map,marker);
    }
    $(window).bind('gMapsLoaded', initialize);
    window.loadGoogleMaps();
}


$(document).on('pageinit', function() {
	$("#checkLoc" ).on("pagehide",function(event,ui){
		$("#checkMsg").html('');
	});	
});
</script>
</style>
</head>
<body>
<div data-role="page" id='index'>
	<div data-role="header" data-position="fixed">
		<div data-role="navbar">
			<ul>
				<li><a href="index.php" rel="external" data-role="button">Check My Position</a>
				<li><a href="#checkLoc" data-rel="dialog" data-role="button">Somebody's Last Location</a>
			</ul>
		</div>
	</div>
	<div data-role="content">		
		<div id="msg" style="text-align:center;font-size:70%;"><?php echo $alias;?></div>
		<div id="map_canvas" style="margin:auto;width:80%;height:320px;"></div>
		<div id='1' style="display:none"></div>
		<div id='2' style="display:none"></div>
	</div>
</div>

<?php echo $checkLocHtml;?>

</body>
</html>